package com.cqupt.utils;

import com.cqupt.pojo.Type;

import java.util.LinkedList;
import java.util.List;

public class KnowledgeUtils {

    /**
     * 返回知识分类的树形结构
     * @param types 所有的分类
     * @param id 分类id
     * @param level 过滤的level
     * @return 知识分类的树形结构
     */
    public static List<Type> transformData(List<Type> types, int id, int level) {
        List<Type> list = new LinkedList<>();
        for (Type type : types) {
            if (type.getTypeLevel() > level) continue;
            if (type.getTypePId() == id) {
                type.setChildren(transformData(types, type.getTypeId(), level));
                list.add(type);
            }
        }
        for (Type type : list) {
            if (type.getChildren().size() == 0){
                type.setChildren(null);
            }
        }
        return list;
    }
}
